package com.wei.chatroom.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @projectName: java_chatroom
 * @package: com.wei.chatroom.controller
 * @className: TestWebSocketController
 * @author: weiJiang
 * @description: 测试一下websocket的用法,插入的函数都是需要重写的方法（直接生成即可）
 * @date: 2025/9/30 下午4:12
 * @version: 1.0
 */

@Component
public class TestWebSocketController extends TextWebSocketHandler {

    //建立成功后自动调用，后面的自己看函数名理解
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
        System.out.println("TestController 连接成功");
    }


    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        super.handleTransportError(session, exception);
        System.out.println("testController 连接异常");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        System.out.println("testController 连接关闭");
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
        System.out.println("TestController 收到消息" + message.toString());
        session.sendMessage(message);
    }
}
